package com.aruba.room;

import android.content.Context;
import android.os.AsyncTask;
import android.view.View;

import androidx.lifecycle.LiveData;

import java.util.List;

/**
 * Created by aruba on 2021/9/12.
 */
public class UserRepository {
    private UserDao userDao;

    public UserRepository(Context context) {
        this.userDao = MyDataBase.init(context.getApplicationContext()).getUserDao();
    }

    public void insert(User user) {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... voids) {
                userDao.insertUser(user);
                return null;
            }
        }.execute();
    }

    public void update(User user) {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... voids) {
                userDao.updateUser(user);
                return null;
            }
        }.execute();
    }

    public void delete(User user) {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... voids) {
                userDao.deleteUser(user);
                return null;
            }
        }.execute();
    }

    public void deleteAllUser() {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... voids) {
                userDao.deleteAllUser();
                return null;
            }
        }.execute();
    }

    public LiveData<List<User>> query() {
        return userDao.getUsers();
    }
}
